//床位管理
function addBedInfo(roomNo) {
    var str="";
    $.ajax({
        url:getRootPath_web()+'baseData/bed/obtainBedData'
        ,type:'post'
        ,dataType:'json'
        ,data:{"roomNo":roomNo}
        ,success:function (result) {
            if(result.code=='0000'){
                str="<div class=\"pop-up\">";
                str+="<div>";
                str+="<div class=\"pop-title relative overflow mb15px\">";
                str+="<span class=\"ml15px\">床位管理</span></span>";
                str+="<p class=\"right closeBtn\" style='cursor: pointer'><img src='"+getRootPath_web()
                    +"static/img/close_icon.png' class=\"block pop-close\"><p>";
                str+="</div>";
                if(result.bedCount>0){
                    str+="<nav class=\"mt30px mb30px\">";
                    str+="<input id='roomNo' type='hidden' value='"+roomNo+"'>";
                    str+="<div class=\"overflow\">";
                    str+="<em class=\"left bgGreen green border-radius4 mr10px pl5px\">色块</em>";
                    str+="<span class=\"left\">已入住不可修改删除</span>";
                    str+="</div>";
                    str+="<ul class=\"add-bed-select mt15px overflow lh32px textCenter bedUl\">";
                    $.each(result.bedList,function (key,b) {
                        if(b.bedEmpty==0){
                            str+="<li id='"+b.bedNo+"'>";
                            str+="<p id='p"+b.bedNo+"'>"+b.bedName+"</p>";
                            str+="<div class=\"overflow\">";
                            str+="<span data-id='"+b.bedNo+"' onclick='updateBed(this)'><img src='"+getRootPath_web()
                                +"static/img/bed_bj.png' ></span>";
                            str+="<span data-id='"+b.bedNo+"' onclick='deleteBed(this)'><img src='"+getRootPath_web()
                                +"static/img/bed_delete.png' ></span>";
                            str+="<div class=\"overflow\">";
                            str+="</div>";
                            str+="</li>";
                        }else {
                            str+="<li class=\"current\">"+b.bedName+"</li>";
                        }
                    })
                    str+="<p class=\"left\" onclick='saveOneBed()'><img src='"+getRootPath_web()+"static/img/add_bed_big.png' class=\"block addBedInfoP\"></p>";
                    str+="</ul>";
                    str+="</nav>";
                }else {
                    str+="<div class=\"pop-form mt30px mb30px\">";
                    str+="<div class=\"layui-form-item lh32px\">";
                    str+="<label class=\"left\">床位数量：</label>";
                    str+="<span class=\"left\" style=\"width: 242px;\">";
                    str+="<input type=\"text\" oninput = \"value=value.replace(/[^\\d]/g,'')\" maxlength='3' placeholder=\"床位数量\" class=\"layui-input bedCount\">";
                    str+="</span>";
                    str+="<span class=\"right white bgGreen border-radius4 pl10px pr10px span-submit\">生成床位编号</span>";
                    str+="</div>";
                    str+="</div>";
                }
                str+="<div>";
            }
            layui.use(['layer'], function() {
                var layer = layui.layer;
                layer.open({
                    type: 1,
                    title: false, //不显示标题栏
                    closeBtn: false,
                    area: ['540px', ''],
                    shade: 0.6,
                    id: 'payCons', //设定一个id，防止重复弹出
                    /*,btn: ['确认支付']*/
                    btnAlign: 'c',
                    moveType: 1, //拖拽模式，0或者1
                    shadeClose: false, //设置允许点遮罩关闭
                    content: str,
                    success: function() {
                        //关闭弹层
                        $(".closeBtn").click(function() {
                            layer.closeAll();
                        });
                        $(".span-submit").click(function () {
                            var bedCount = $(".bedCount").val();
                            if(bedCount==''){
                                layer.msg("请输入床位数量！");
                                return false;
                            }
                            saveBedInfo(roomNo,bedCount);
                        })
                    },
                });
            });
        }
    })
}
/**
* 
* 功能描述：保存床位信息
*
* @Author  GZ
* @Date   2020/11/8 19:34
*/
function saveBedInfo(roomNo,bedCount) {
    var floorNo =$("#floorNo").val();
    var loading;
    layui.use('layer', function(){
        var layer = layui.layer;
        // layer.confirm('确定创建床位吗？', {
        //     btn: ['确定','取消'] //按钮
        // }, function(){
        //
        // }, function(){
        //     layer.close(layer.index);
        // });
        $.ajax({
            url:getRootPath_web()
                +'baseData/bed/saveBedInfo',
            type:'post',
            dataType:'json',
            data:{"roomNo":roomNo,"bedCount":bedCount},
            beforeSend:function () {
                loading = layer.load(1, {
                    shade: [0.9,'#fff']
                    ,shadeClose:false
                });
            },
            success:function (result) {
                if(result.code=='0000'){
                    layer.confirm('添加成功', {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.closeAll();
                        window.location.href=getRootPath_web()+"baseData/room/roomList?floorNo="+floorNo;
                    })
                }else {
                    layer.confirm(result.codeDesc, {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.close(layer.index);
                        layer.close(loading);
                    })
                }
            }
        })
    });
}
//添加单个床位
function saveOneBed() {
    var roomNo=$("#roomNo").val();
    var loading;
    layui.use('layer', function(){
        var layer = layui.layer;
        // layer.confirm('确定创建床位吗？', {
        //     btn: ['确定','取消'] //按钮
        // }, function(){
        //
        // }, function(){
        //     layer.close(layer.index);
        // });
        $.ajax({
            url:getRootPath_web()
                +'baseData/bed/saveBedInfo',
            type:'post',
            dataType:'json',
            data:{"roomNo":roomNo,"bedCount":1},
            beforeSend:function () {
                loading = layer.load(1, {
                    shade: [0.9,'#fff']
                    ,shadeClose:false
                });
            },
            success:function (result) {
                if(result.code=='0000'){
                    layer.confirm('添加成功', {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        $(".addBedInfoP").remove();
                        var str="";
                        str+="<li id='"+result.bedNo+"'>";
                        str+="<p id='p"+result.bedNo+"'>"+result.bedName+"</p>";
                        str+="<div class=\"overflow\">";
                        str+="<span data-id='"+result.bedNo+"' onclick='updateBed(this)'><img src='"+getRootPath_web()
                            +"static/img/bed_bj.png' ></span>";
                        str+="<span data-id='"+result.bedNo+"' onclick='deleteBed(this)'><img src='"+getRootPath_web()
                            +"static/img/bed_delete.png' ></span>";
                        str+="<div class=\"overflow\">";
                        str+="</div>";
                        str+="</li>";
                        str+="<p class=\"left\" onclick='saveOneBed()'><img src='"+getRootPath_web()+"static/img/add_bed_big.png' class=\"block addBedInfoP\"></p>";
                        $(".addBedInfoP").remove();
                        $(".bedUl").append(str);
                        layer.close(layer.index);
                        layer.close(loading);
                    })
                }else {
                    layer.confirm(result.codeDesc, {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.close(layer.index);
                        layer.close(loading);
                    })
                }
            }
        })
    });
}
//修改床位信息
function updateBed(object) {
    var bedNo = $(object).attr("data-id");
    $.ajax({
        url:getRootPath_web()+'baseData/bed/queryBedInfo',
        dataType:'json',
        type:'post',
        data:{"bedNo":bedNo},
        success:function (result) {
            layui.use('layer', function(){
                var layer = layui.layer;
                layer.prompt({title: '床位修改', formType: 0,value:result.bedName}
                , function(text, index){
                    //验证床位信息
                    var numberTest =/^\+?[1-9][0-9]*$/;
                    if(!numberTest.test(text)){
                        layer.msg("房间号只能为数字！");
                        return false;
                    }
                    $.ajax({
                        url:getRootPath_web()+'baseData/bed/updateBedInfo',
                        dataType:'json',
                        type:'post',
                        data:{"bedNo":bedNo,"bedName":text},
                        success:function (resultData) {
                            if(resultData.code=='0000'){
                                layer.close(index);
                                $("#p"+bedNo).html(text);
                            }else {
                                layer.msg(resultData.codeDesc);
                            }
                        }
                    })
                });
            });
        }
    })
}

//删除床位信息
function deleteBed(object) {
    var bedNo = $(object).attr("data-id");
    layui.use('layer', function(){
        var layer = layui.layer;
        layer.confirm('确定删除该床位吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                url:getRootPath_web()
                    +'baseData/bed/deleteBedInfo',
                type:'post',
                dataType:'json',
                data:{"bedNo":bedNo},
                success:function (result) {
                    if(result.code=='0000'){
                        layer.confirm('删除成功', {
                            btn: ['确定'] //按钮
                            ,shadeClose: false
                        }, function(){
                            $("#"+bedNo).remove();
                            layer.close(layer.index);
                        })
                    }else {
                        layer.confirm(result.codeDesc, {
                            btn: ['确定'] //按钮
                            ,shadeClose: false
                        }, function(){
                            layer.close(layer.index);
                        })
                    }
                }
            })
        }, function(){
            layer.close(layer.index);
        });
    });
}